<?php

namespace application\Controller;

use application\Components\BaseController;
use application\Comm\Captcha;
use application\Comm\Request;
use application\Module\Adimage;
use application\Module\Adtext;
use application\Module\Buying;
use application\Module\Comment;
use application\Module\LoginForm;
use library\Core\Config;
use application\Comm\WaterMask;
use library\Core\Db\CDbCriteria;
use application\Model\CommentModel;
use application\Module\Entitystores;
use application\Comm\Func;
use application\Module\House;
use application\Module\Zxadimg;
use application\Module\Zxarticle;

class SiteController extends BaseController
{
	
	/**
	 *
	 * @throws \application\Components\CException 首页
	 */
	public function actionIndex() {
		// 读取所有首页图片广告
		$adimage = new Adimage ();
        $ad_image_pid = Config::getParamsVal('AD_IMAGE_POSITION');
        
        $adImageList = $adimage->getList(array_keys($ad_image_pid));
        $ad_image_list = [];
        foreach ($adImageList as $row) {
            $ad_image_list[$row['position_id']][] = $row;
        }
        
		//Func::pre($ad_image_list);
        //读取所有首页文字广告
        $adtext = new Adtext();
        $ad_text_pid = Config::getParamsVal('AD_TEXT_POSITION');
        
        $adTextList = $adtext->getList(array_keys($ad_text_pid));
        
        $ad_text_list = [];
        foreach ($adTextList as $row) {
        	$ad_text_list[$row['position_id']][] = $row;
            
        }
        //读取限间抢购
        $buying = new Buying();
        $buying_list = $buying->getList();
        //读取评论
        $comment = new Comment();
        $comment_pid = [
            1 => '产品评论',
            2 => '物流评论',
            3 => '服务评论',
        ];
        $comment_list = [];
        foreach ($comment_pid as $k => $v) {
            $comment_list[$k] = $comment->getList($k);
        }

		//three  index img 首页报名下的三张图
		$Adimage = new Adimage();
		$imgthree = $Adimage->getList(18);
		//为您推荐
// 		$Recdata = $Adimage->getProduct(26);
		//风格速递
		$select1 = ['id', 'name', 'price','style_id'];
		$styledata = $Adimage->getProduct(27,$select1);
		//功能定制
		$functiondata = $Adimage->getProduct(28,['id','name','category_id','function_id']);
		//功能定制文字广告
		$funtextmodel = new Adtext();
		$functiontext = $funtextmodel->getList(16);
		
		//全屋设计
		$housemodule = new House();
		$housedata = $housemodule->getIndex();
		//Func::pre($housedata);
		//家居小知识
// 		$zxadimg = new Zxadimg();
// 		$zdata = $zxadimg->getAdList(56);
		
		//评论
		$commentmoduel = new Comment();
		$datacomment = $commentmoduel->getList();
		
        $this->render('site/index',
            [
            	'datacomment'=>$datacomment,
//             	'zdata'=>$zdata,
            	'housedata'=>$housedata,
            	'functiontext'=>$functiontext,
            	'functiondata'=>$functiondata,
            	'styledata'=>$styledata,
//             	'Recdata'=>$Recdata,
            	'imgthree' =>$imgthree,
                'ad_image_list' => $ad_image_list,
                'ad_text_list' => $ad_text_list,
                'buying_list' => $buying_list,
                'comment_list' => $comment_list,
            ]);
    }

/* 废弃的登录模块 */
    /*
    public function actionLogin()
    {
        $errors = [];
        $loginParam = Request::postParam('info');

        if ($loginParam) {

            $loginForm = new LoginForm();
            $result = $loginForm->login($loginParam);
            if ($result['status'] == true) {
                $this->redirect(['site/index']);
            }
            $errors = $result['errors'];
        }
        $this->renderPartial('site/login', [
            'errors' => $errors,
        ]);
    }
*/
/*
 * 验证码、登出（废弃）
    public function actionCaptcha()
    {
        $captcha = new Captcha(); //实例化验证码 
        $captcha->width = 95;
        $captcha->height = 33;
        $captcha->doimg(); //生成验证码图片
    }

    public function actionLogout()
    {
        session_destroy();
        $this->redirect(['site/login']);
    }
*/
    public function actionError()
    {
        echo "Error!";
    }
/**
    public function actionUpload()
    {
        $d = Request::getParam('d');
        $dir_arr = [
            1 => 'product/',
            2 => 'other/',
        ];
        //指定目录存放
        if ($d && isset($dir_arr[$d])) {
            $dir = $dir_arr[$d];
        } else {
            $dir = 'other/';
        }
        if (isset($_FILES['file'])) {
            $picname = $_FILES['file']['name'];
            $picsize = $_FILES['file']['size'];
            if ($picname != "") {
                if ($picsize > 1024000 * 3) {
                    echo '图片大小不能超过3M';
                    exit;
                }
                $type = strstr($picname, '.');
                if ($type != ".gif" && $type != ".jpg" && $type != ".png" && $type != ".jpeg") {
                    echo '图片格式不对！（支持格式PNG,jpg,gif）';
                    exit;
                }
                $rand = rand(10000, 99999);
                $pics = date("YmdHis") . $rand . $type;
                //上传路径
                $year = date('Y', time()) . '/';
                $mouth = date('m', time()) . '/';
                $day = date('d', time()) . '/';
                $return_path = $dir . $year . $mouth . $day;
                if (!is_dir(Config::getParamsVal('UPLOAD_DIR') . 'public/' . $return_path)) {
                    mkdir(Config::getParamsVal('UPLOAD_DIR') . 'public/' . $return_path, 0777, true);
                }
                if (!is_dir(Config::getParamsVal('UPLOAD_DIR') . 'source/' . $return_path)) {
                    mkdir(Config::getParamsVal('UPLOAD_DIR') . 'source/' . $return_path, 0777, true);
                }
                //处理过文件（对外）
                $public_file = Config::getParamsVal('UPLOAD_DIR') . 'public/' . $return_path . $pics;
                //源文件（不对外）
                $source_file = Config::getParamsVal('UPLOAD_DIR') . 'source/' . $return_path . $pics;
                move_uploaded_file($_FILES['file']['tmp_name'], $source_file);
                copy($source_file, $public_file);

                if (Request::getParam('iswater') == 1) {
                    $water = new WaterMask($public_file);
                    $water->waterType = 1;
                    $water->output();
                }
            }

            $http_status = ['statusCode' => 200, 'filename' => $return_path . $pics];
            echo json_encode($http_status);
        } else {
            echo '非法请求';
            exit;
        }

    }
*/
    /**
     * @throws \application\Components\CException
     * 报名
     */
    public function actionApply()
    {
        $this->render('site/apply', []);
    }

    /**
     * @throws \application\Components\CException
     * 定制流程
     */
    public function actionFlow()
    {
        $this->render('site/flow', []);
    }
    
    /**
     * @throws \application\Components\CException
     * 实体门店
     */
    public function actionStores()
    {
    	//$cityname = Request::getParam('cityname');
    	$province = Request::postParam('province') ? Request::postParam('province') : Request::getParam('province');
    	$city = Request::postParam('city') ? Request::postParam('city') : Request::getParam('city');
    	
    	$select = [
    			'id',
    			'name',
    			'longitude',
    			'latitude',
    			'phone',
    			'area_fix',
    			'address'
    			];
    	$model = new Entitystores();
    	$data = $model->getList($province,$city,$select);
    	$address = [];
    	$arr = [];
    	if (isset($data['data'][0])){
    		$arr = explode('.', $data['data'][0]['area_fix']);
    		$address = (count($arr) == 1 && substr_count($arr[0],'·') >0) ? explode('·', $data['data'][0]['area_fix']) : $arr;
    	}
    	
    	if ($province =='' && $city==''){
    		$address[0] = '全国';
    	}
    	
    	$this->render('site/stores', [
    		'address' => isset($address[0]) ? $address[0] : '全国',
    		'data' => $data['data'],
    		'page' => $data['page'],
    		'province' => $province,
    		'city' => $city,
    	]);
    }
    
    /**
     * @throws \application\Components\CException
     * 实体门店
     */
    public function actionDostores()
    {
    	$province = Request::getParam('province');
    	$city = Request::getParam('city');
    	
    	$select = [
    		'id',
    		'name',
    		'longitude',
    		'latitude',
    		'phone',
    		'area_fix',
    		'address',
    	];
    	$model = new Entitystores();
    	$data = $model->getList($province, $city, $select);
    	echo json_encode($data);
    	
    }

    /**
     * @throws \application\Components\CException
     * 品牌实力
     */
    public function actionBrand()
    {
        $this->render('site/brand', []);
    }
    

    /**
     * @throws \application\Components\CException
     * 七星定制
     */
    public function actionqixing()
    {
    	$this->render('site/qixing', []);
    }

    public function actionLike($id)
    {
        $commentModule = new Comment();
        $like = $commentModule->getLike($id);
        $like++;
        $criteria = new CDbCriteria();
        $criteria->update(['like'=>$like]);
        $criteria->addEqCondition('id', $id);
        $commentModel = new CommentModel();
        $data = $commentModel->modify($criteria);
        if ($data) {
            $httpStatus = [
                'statusCode' => 200,
                'message' => 'ok',
            ];
        } else {
            $httpStatus = [
                'statusCode' => 300,
                'message' => 'error',
            ];
        }
        echo json_encode($httpStatus);
    }
    
    public function actionRelation(){
    	
    	$this->render('site/relation', []);
    }

}
